package com.xiaowu.datajpa.api;

import com.xiaowu.datajpa.constant.ResultEnum;
import com.xiaowu.datajpa.entity.Student;
import com.xiaowu.datajpa.exception.ResultException;
import com.xiaowu.datajpa.utils.PageUtil;
import com.xiaowu.datajpa.vo.QueryObj;
import com.xiaowu.datajpa.vo.result.Result;
import com.xiaowu.datajpa.service.StudentSercvice;
import com.xiaowu.datajpa.utils.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学生管理")
@RestController
@RequestMapping("/api/student")
public class StudentApiController {
    @Autowired
    private StudentSercvice studentSercvice;
    @GetMapping("/list")
    public Result<List<Student>> getList(){
        List<Student> students = studentSercvice.findAll();
        return ResultUtil.success(students);
    }

    @GetMapping("/search")
    public Result<List<Student>> findByNameLike(@Param("name") String name){
    	List<Student> students = studentSercvice.findByNameLike(name+"%");
    	return ResultUtil.success(students);
    }

    @GetMapping("/{id}")
    public Result<Student> findById(@PathVariable Integer id) {
        return ResultUtil.success(studentSercvice.findById(id));
    }
    @PostMapping("/add")
    public Result<Student> add(Student student) {
    	return ResultUtil.success(studentSercvice.insert(student));
    }

    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        studentSercvice.delete(ids);
        return ResultUtil.success();
    }

    @PutMapping("/update")
    public Result<Student> update(Student student) {
        return ResultUtil.success(studentSercvice.update(student));
    }

    //重置密码
    @GetMapping("/reset/{ids}")
    public Result resetPassword(@PathVariable List<Integer> ids){
        studentSercvice.resetPassword(ids);
        return ResultUtil.success();
    }
    //验证学号和密码
    @GetMapping("/validate")
    public Result<Student> validateStumberAndPassword(String stunumber,String password)throws Exception{
        Student student = studentSercvice.findByStumberAndPassword(stunumber, password);
        return ResultUtil.success(student);
    }

    @PostMapping("/getbypage")
    public Result<Page<Student>> getByPage(@RequestBody(required = false) QueryObj<Student> qobj){
        //按id排序
           /* Sort sort = Sort.by(Sort.Direction.DESC,"id");
            Integer pageIndex=0;
            Integer pageSize=10;
            if(qobj==null){
                Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
                Page<Student> students = studentSercvice.getAll(pageable);
                return ResultUtil.success(students);
            }else {
                pageIndex=qobj.getPage();
                pageSize=qobj.getLimit();
                if(qobj.getData() instanceof Student){
                    Student student = qobj.getData();
                    Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
                    ExampleMatcher matcher = ExampleMatcher.matching()
                            .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                            .withIgnoreNullValues();
                    Example<Student> example = Example.of(student, matcher);
                    Page<Student> students = studentSercvice.getAll(example,pageable);
                    return ResultUtil.success(students);
                }else{
                    throw new ResultException(ResultEnum.Qurey_ERR);
                }
            }*/
        if(qobj==null){
            Pageable pageable = PageUtil.getPageable(qobj);
            Page<Student> students = studentSercvice.getAll(pageable);
            return ResultUtil.success(students.getContent(),students.getTotalElements());
        }else{
            if(qobj.getData() instanceof Student){
                Pageable pageable = PageUtil.getPageable(qobj);
                Example<Student> example = PageUtil.getExample(qobj);
                Page<Student> students = studentSercvice.getAll(example,pageable);
                return ResultUtil.success(students.getContent(),students.getTotalElements());
            }else{
                throw new ResultException(ResultEnum.Qurey_ERR);
            }

        }
    }
}
